import BaseView from "../baseView.js";

export default class LoginView extends BaseView {
    render() {
        this.$el.html(`
      <h1 style='margin-left:700px'>管理员登录界面</h1>
       <hr/>  
    <div style='width:300px;margin:100px auto'>
    <form class="layui-form">
    <div class="layui-form-item">
    <label class="layui-form-label">用户名：</label>
    <div class="layui-input-block">
      <input type="text" id="username" required placeholder="请输入用户名" class="layui-input" value='123'>
    </div>
     </div>
     <div class="layui-form-item">
    <label class="layui-form-label">密码：</label>
    <div class="layui-input-block">
      <input type="password"  id="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input" value='123'>
    </div>
    <div class="layui-form-item">
    <br/>
    <div class="layui-input-block">
      <button type='button' id='loginBtn' class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
      <button type="button" id='regBtn' class="layui-btn layui-btn-primary">注册</button>
    </div>
    </div>
    </form>
    </div>
      `)
    }
    handle() {
        $("#loginBtn").click(() => {
            // console.log(1111);
            $.ajax({
                type: "post",
                url: "/admins/login",
                data: {
                    "username": $("#username").val(),
                    "password": $("#password").val()
                },
                dataType: "json",
                success: (data) => {
                    console.log(data);
                    if (data.code == 1) {
                        alert(data.msg);
                        localStorage.setItem("_t",data._t);
                        window.location.hash = "#/main";
                    } else {
                        alert(data.msg);
                        //清空输入框
                        $("#username").val("");
                        $("#password").val("")
                    }
                }
            })
        });
        //注册
        $("#regBtn").click(() => {
            window.location.hash = "#/reg";
        })
    }
}